<?php
/**
 * rakuten订单请求配置 (乐天)
 * (rakuten-model)
 */

namespace App\Models\Rakuten;

use Illuminate\Database\Eloquent\Model;

class RakutenOrderCondition extends Model {

    protected $connection = 'mysql_rakuten';

    protected $table = 'rakuten_order_condition';

    protected $guarded  = [];

    /**
     * 获取执行时间 (上次请求时间)
     * @param int $shop
     * @param int $account_id
     * @return mixed
     */
    public static function getNextStartTime($shop = 1, $account_id = 200000){
        return self::query()
            ->where('shop',$shop)
            ->where('account_id',$account_id)
            ->value('next_start_time');
    }

    /**
     * 更新执行时间
     * @param $nextStartTime
     * @param int $shop
     * @param int $account_id
     * @return \Illuminate\Database\Eloquent\Builder|Model
     */
    public static function upNextStartTime($nextStartTime, $shop = 1, $account_id = 200000){
        return self::query()->updateOrCreate([
            'shop' => $shop,
            'account_id' => $account_id
        ], [
            'next_start_time' => $nextStartTime
        ]);
    }

    public static function rakutenForApi($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->where('status',1)->get();
    }

}
